package com.ce.server.controller;


import cn.hutool.core.util.StrUtil;
import com.ce.server.common.RespBean;
import com.ce.server.common.RespPageBean;
import com.ce.server.pojo.Admin;
import com.ce.server.pojo.ClockIn;
import com.ce.server.pojo.Operlog;
import com.ce.server.pojo.Student;
import com.ce.server.service.IClockInService;
import com.ce.server.service.IOperlogService;
import com.ce.server.util.AdminUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 郑爽
 * @since 2021-11-17
 */
@RestController
@RequestMapping("/clock-in")
public class ClockInController {
    @Autowired
    private IClockInService clockInService;
    @Autowired
    private IOperlogService operlogService;

    @Value("${type.student}")
    private String type;



    @ApiOperation(value = "提交打卡信息")
    @PostMapping("/")
    public RespBean commitClockInMessage(@RequestBody ClockIn clockIn) {
        if (clockInService.commitClockInMessage(clockIn)) {
            return RespBean.success("打卡成功!");
        }
        return RespBean.error("打卡失败!");
    }

    @ApiOperation(value = "检测今天是否已经打卡")
    @GetMapping("/check")
    public RespBean checkTodayIsClockIn() {
        //获取当前登录的账号
        Student student = (Student) AdminUtil.getCurrentAdmin();
        //获取学生最近的一次打卡记录
        ClockIn clockin = clockInService.getClockInByToday(student.getSno());
        if (clockin!=null && !StrUtil.hasEmpty(clockin.getTemperature())) return RespBean.error("今天已打卡过了!");
        return RespBean.success("今天还没有打卡!");
    }

    @ApiOperation(value = "获取今天的打卡信息")
    @GetMapping("/today")
    public RespBean getTodayClockInMessage() {
        //获取当前登录的账号
        Student student = (Student) AdminUtil.getCurrentAdmin();
        //获取学生最近的一次打卡记录
        ClockIn clockin = clockInService.getClockInByToday(student.getSno());

        if (clockin != null)
            return RespBean.success("获取今日打卡信息成功!", clockin);
        return RespBean.error("获取今日打卡信息失败!");
    }

    @ApiOperation(value = "管理员根据学号获取学生今日打卡信息")
    @GetMapping("/today/{sno}")
    public RespBean getTodayClockInMessage(@PathVariable("sno") String sno) {
        //获取学生今天的打卡记录
        ClockIn clockin = clockInService.getClockInByToday(type + sno);

        if (clockin != null)
            return RespBean.success("获取今日打卡信息成功!", clockin);
        return RespBean.error("获取今日打卡信息失败!");
    }

    @ApiOperation(value = "分页获取所有打卡信息")
    @GetMapping("/")
    public RespPageBean getClockInMessageBySno(@RequestParam(defaultValue = "1") Integer currentPage,
                                               @RequestParam(defaultValue = "10") Integer size,
                                               ClockIn clockIn, LocalDate[] createTimeScope) {

        RespPageBean clockInsByPage = clockInService.getClockInsByPage(currentPage, size, clockIn,createTimeScope);
        if (clockInsByPage != null) return clockInsByPage;
        return new RespPageBean(0L, null);
    }

    @ApiOperation(value = "获取今日已打卡的学生信息")
    @GetMapping("/today/ClockIn")
    public RespPageBean getStudentsFromTodayClockIn(@RequestParam(defaultValue = "1") Integer currentPage,
                                                    @RequestParam(defaultValue = "10") Integer size) {
        RespPageBean clockInsByPage = clockInService.getTodayClockInsByPage(currentPage, size, true);
        if (clockInsByPage != null) return clockInsByPage;
        return new RespPageBean(0L, null);
    }

    @ApiOperation(value = "获取今日未打卡的学生信息")
    @GetMapping("/today/noClockIn")
    public RespPageBean getStudentsFromTodayNoClockIn(@RequestParam(defaultValue = "1") Integer currentPage,
                                                      @RequestParam(defaultValue = "10") Integer size) {
        RespPageBean clockInsByPage = clockInService.getTodayClockInsByPage(currentPage, size, false);
        if (clockInsByPage != null) return clockInsByPage;
        return new RespPageBean(0L, null);
    }

    @ApiOperation(value = "批量删除打卡信息")
    @DeleteMapping("/")
    public RespBean delRecords(Integer[] ids){
        Admin currentAdmin = (Admin) AdminUtil.getCurrentAdmin();
        operlogService.save(new Operlog().setWno(currentAdmin.getWno())
                .setWname(currentAdmin.getWname())
                .setOper("批量删除打卡信息").setOpertime(LocalDateTime.now()));
        return clockInService.delRecords(ids);
    }
}
